<?php
namespace Common\Model;

use Think\Model;

class InfoContentModel extends Model
{

    protected $_validate = array(
        array('cid', 'require', '栏目不能为空'),
        array('title', 'require', '标题不能为空'),
        array('address', 'require', '区域未选择'),
        array('content', 'require', '内容不能为空'),
        array('overdate', 'require', '有效期不能为空'),
        array('adminpassword', 'require', '管理密码不能为空'),
    );

    public function getList(){
        $p = I('p',0,'int');
        $limit = C('DEFALUT_PAGE_SIZE');
        $startNum = $p * $limit;
        $count = $this->field("count(*) as count")->find();
        $data = $this->limit($startNum,$limit)->order('create_time desc')->select();
        return array(
            'count' => $count['count'],
            'data'  => $data
        );
    }

    public function addInfoContent()
    {
        if (!$this->create()) {
            return false;
        } else {
            if (empty(I('tel')) && empty(I('phone')) && empty(I('qq'))) {
                $this->error = "联系方式不能都为空";
                return false;
            }
            $data['pic'] = I("pic");
            if(!empty($data['pic'])){
                $temp = array_filter(explode(',',$data['pic']));
                $pic = array();$fullPath = array();
                foreach($temp as $k => $v){

                    if(empty($v)) continue;
                    if($k >= 5) break;
                    $fullPath[] = $v;
                    $pic[] = basename($v);
                }
                if($pic){
                    $data['thum'] = $pic[0];
                    $pic = implode(',',$pic);
                    $data['images'] = $pic;
                }
            }else{
                $data['thum'] = '';
                $data['images'] = '';
            }
            $data['status'] = C("cfg_info_verify");
            $data['cid'] = I('cid');
            $data['title'] = I("title");
            $data['phone'] = I('phone');
            $data['tel'] = I('tel');
            $data['qq'] = I('qq');
            $data['area'] = I('address');
            $data['linkman'] = I('name');
            $data['money'] = I('price');
            $data['content'] = I('content');
            $data['valid_date'] = date('Y-m-d', strtotime(I('overdate') . ' day'));
            $data['password'] = I('adminpassword');
            $uid = cookie(C('COOKIEVALIDATIONKEY'));
            $uid = !empty($uid) ? explode('_',$uid)[0] : 0;
            $data['user_id'] = $uid ;
        }
        $return = $this->add($data);
        return $return;
    }


    public function getInfoInCidArr($cidArr,$limit=null){
        $limit = $limit == null ? 10 : $limit;
        if(is_array($cidArr)){
            $map['cid'] = array('in',$cidArr);
        }else if($cidArr == 'top'){
            $map['type'] =  1;
        }else if($cidArr == 'thum'){
            $map['thum'] =  array('neq','');
        }else{
            $map =  '';
        }
        $map['status'] = 1;
        $field = array('id,title,area,linkman,money,content,cid,phone,tel,valid_date,thum,type');
        return $this->where($map)->field($field)->order('create_time desc')->limit($limit)->select();
    }



    public function editCategory()
    {
        $cid = I('get.cid');
        if ($_POST['name'] == '') {
            $this->error = '栏目名称不能为空';
            return false;
        }
        $category = M("category")->where("cid ='{$cid}'")->save($_POST);
        if ($category) {
            return true;
        } else {
            $this->error = "栏目更新失败";
            return false;
        }
    }


    public function delCategory()
    {
        $cid = I('get.cid');
        $mid = i('get.mid');
        $modelField = M('model')->where("mid = '{$mid}'")->find();
        //删除栏目对应的文章 默认表
        $default = M('defaultonmodel')->where("cid='{$cid}'")->delete();
        //删除独立表文章
        if ($modelField['mmodel_name']) { //如果独立表模型存在
            $selfModel = M($modelField['mmodel_name'])->where("cid='{$cid}'")->delete();
        }
        //删除栏目
        $result = M('category')->where("cid='{$cid}'")->delete();
        if ($result) {
            return true;
        } else {
            $this->error = "删除失败";
            return false;
        }
    }

    public function getInfoData($pid, $cid, $area, $m1, $m2)
    {
        $p = I('p', 1, 'int');
        $date = date('Y-m-d', time());
        $field = array('id,title,area,linkman,money,content,cid,phone,tel,valid_date,thum,type,create_time');
        $map['valid_date'] = array('EGT', $date);
        $map['status'] = array('eq', 1);
        if (!empty($pid)) {
            if (empty($cid)) {
                $cidList = M('info_category')->where("pid={$pid}")->field("id")->select();
                $cidList = array_column($cidList, 'id');
                $cidList[] = $pid;
                $map['cid'] = array('in', $cidList);
            } else {
                $map['cid'] = array('eq', $pid);
            }
        }
        $cid = $cid == '-1' ? '' : $cid;
        if (!empty($cid)) {
            $map['cid'] = array('eq', $cid);
        }
        if (!empty($pid) && !empty($cid)) {
            $map['cid'] = array('in', array($pid, $cid));
        }
        $area = $area == '-1' ? '' : $area;
        if (!empty($area)) {
            $area = (int)$area;
            $map['area'] = array('eq', $area);
        }
        if ($m1 >= 0 && !empty($m2)) {
            $map['money'] = array('BETWEEN', array($m1, $m2));
        }

        $count = $this->where($map)->field("count(*) as count")->order('type desc')->find();
        $page = ($p - 1) * C('PAGE_SIZE');
        $data = $this->where($map)->field($field)->order('type desc,create_time desc')->limit($page, C('PAGE_SIZE'))->select();
        $return = array(
            'count' => $count['count'],
            'data' => $data
        );
        return $return;
    }

    public function getInfoDataByUid($uid)
    {
        $p = I('p', 1, 'int');
        $field = array('id,title,area,linkman,money,content,cid,phone,tel,valid_date,thum');
        $map['uid'] = array('eq', $uid);


        $count = $this->where($map)->field("count(*) as count")->order('type desc')->find();
        $page = ($p - 1) * C('PAGE_SIZE');
        $data = $this->where($map)->field($field)->order('type desc,create_time desc')->limit($page, C('PAGE_SIZE'))->select();
        $return = array(
            'count' => $count['count'],
            'data' => $data
        );
        return $return;
    }

    public function getInfosByPhone($phone)
    {
        $map['phone'] = array('eq', (int)$phone);
        $times = $this->where($map)->field("count(*) as count")->find();
        $times = $times == true ? $times['count'] : 0;
        return $times;
    }

    public function getInfoInId($cidArr)
    {
        if (!empty($cidArr)) {
            $map['cid'] = array('in', $cidArr);
            $data = $this->where($map)->limit(10)->order('click desc')->select();
        } else {
            $data = array();
        }
        return $data;
    }

    public function getinfoInInfoId($infoIdArr)
    {
        if (!empty($infoIdArr)) {
            $map['id'] = array('in', $infoIdArr);
            $data = $this->where($map)->limit(10)->order('click desc')->select();
        } else {
            $data = array();
        }
        return $data;
    }

    public function getInfoList($uid, $limit)
    {
        return $this->where("user_id = {$uid}")->order('create_time desc')->limit($limit)->select();
    }

    public function updateCreateTime($id)
    {
        $data = array(
            'id' => $id,
            'status' => C('cfg_info_verify'),
            'create_time' => date('Y-m-d H:i:s')
        );
        return $this->save($data);
    }

    public function deleteInfo($id)
    {
        return $this->delete($id);
    }

    public function getInfoById($id)
    {
        return $this->find($id);
    }

    public function getCountByPhone($phone){
        $map['phone'] = $phone;
        return $this->where($map)->field("count(*) as count")->find();
    }

    public function searchByPhone($phone,$page){
        if(!$phone) return array();
        $count = $this->getCountByPhone($phone);
        if(!$phone || !$count['count']){
            return array();
        }
        $startPage = $page * C('PAGE_SIZE_SEARCH');
        $map['phone'] = $phone;
        $infoData = $this->where($map)->field("id,title,content,create_time")->limit($startPage,C('PAGE_SIZE_SEARCH'))->select();

        return $data = array(
            'count' => $count['count'],
            'data'  => $infoData
        );
    }

    public function editeInfo($id){
        if (!$this->create()) {
            return false;
        }
        if (empty(I('tel')) && empty(I('phone')) && empty(I('qq'))) {
            $this->error = "联系方式不能都为空";
            return false;
        }
        $data['pic'] = I('pic');
        if(!empty($data['pic'])){
            $temp = array_filter(explode(',',$data['pic']));
            $pic = array();$fullPath = array();
            foreach($temp as $k => $v){
                if(empty($v)) continue;
                if($k >= 6) break;
                $fullPath[] = $v;
                $pic[] = basename($v);
            }
            if($pic){
                $data['thum'] = $pic[0];
                $pic = implode(',',$pic);
                $data['images'] = $pic;
            }
        }else{
            $data['thum'] = '';
            $data['images'] = '';
        }
        $data['title']  = empty(I('dyff89aso')) ? I('title') : I('dyff89aso');
        $data['area']   = I('address');
        $data['linkman']    = I('name');
        $data['money']  = I('price');
        $data['content']    = I('content');
        $data['status'] = C('cfg_info_verify');
        $data['create_time']    = date("Y-m-d H:i:s",time());
        $data['valid_date'] = date('Y-m-d', strtotime('+'.I('overdate') . ' day'));
        $data['password']   = I('adminpassword');
        $data['qq']     = I('qq');
        $data['tel']    = I('tel');
        $data['phone']  = I('phone');
        $uid = cookie(C('COOKIEVALIDATIONKEY'));
        $uid = !empty($uid) ? explode('_',$uid)[0] : 0;
        $data['user_id'] = $uid ;

        $update = $this->where(array('id'=> $id))->save($data);
        return $update;
    }

    public function getInfoByIds($ids){
        if(!$ids) return array();
        $map['id'] = array('in',$ids);
        $res = $this->where($map)->field("id,title,content,create_time")->select();
        if(!$res) $res = array();
        return $res;
    }

    public function createInfo(){

        if (!$this->create()) {
            return false;
        }
        if(I('post.content') == ''){
            $this->error = "内容不能为空";
            return false;
        }
        if (empty(I('tel')) && empty(I('phone')) && empty(I('qq'))) {
            $this->error = "联系方式不能都为空";
            return false;
        }
        $data = I('post.');
        $data['thum'] = I("thum");

        if(isset($data['thum']) && !empty($data['thum'])){
            $temp = array_filter(explode(',',$data['thum']));
            $pic = array();$fullPath = array();
            foreach($temp as $k => $v){

                if(empty($v)) continue;
                if($k >= 5) break;
                $fullPath[] = $v;
                $pic[] = basename($v);
            }
            if($pic){
                $data['thum'] = $pic[0];
                $pic = implode(',',$pic);
                $data['images'] = $pic;
            }
        }else{
            $data['thum'] = '';
            $data['images'] = '';
        }



        $data['valid_date'] = date('Y-m-d', strtotime(I('valid_date') . ' day'));
        $data['password'] = I('password');
        $uid = cookie(C('COOKIEVALIDATIONKEY'));
        $uid = !empty($uid) ? explode('_',$uid)[0] : 0;
        $data['user_id'] = $uid ;

        $data['content'] = $_POST['content'];
        $res = $this->add($data);
        return $res;
    }

    public function edit(){
        $id = I('get.id');
        if (!$this->create()) {
            return false;
        }
        if(I('post.content') == ''){
            $this->error = "内容不能为空";
            return false;
        }
        if (empty(I('tel')) && empty(I('phone')) && empty(I('qq'))) {
            $this->error = "联系方式不能都为空";
            return false;
        }
        $data = I('post.');
        $data['thum'] = I("thum");

        if(isset($data['thum']) && !empty($data['thum'])){
            $temp = array_filter(explode(',',$data['thum']));
            $pic = array();$fullPath = array();
            foreach($temp as $k => $v){

                if(empty($v)) continue;
                if($k >= 5) break;
                $fullPath[] = $v;
                $pic[] = basename($v);
            }
            if($pic){
                $data['thum'] = $pic[0];
                $pic = implode(',',$pic);
                $data['images'] = $pic;
            }
        }else{
            $data['thum'] = '';
            $data['images'] = '';
        }

        $data['valid_date'] = date('Y-m-d', strtotime( '+' . I('valid_date') . ' day'));
        $data['password'] = I('password');
        $uid = cookie(C('COOKIEVALIDATIONKEY'));
        $uid = !empty($uid) ? explode('_',$uid)[0] : 0;
        $data['user_id'] = $uid ;

        $data['content'] = $_POST['content'];
        $data['id'] = $id;
        $res = $this->save($data);
        return $res;
    }

    public function pass(){
        $id = I('get.id');
        $idArr = array_filter(explode(',',$id));
        $idArr =  array_map('intval', $idArr);
        $data['status'] = 1;
        $map['id'] = array('in',$idArr);
        $res = $this->where($map)->save($data);
        return $res;
    }

    public function del(){
        $id = I('get.id');
        $idArr = array_filter(explode(',',$id));
        $idArr =  array_map('intval', $idArr);
        $map['id'] = array('in',$idArr);
        $res = $this->where($map)->delete();
        return $res;
    }

    public function top(){
        $id = I('get.id');
        $idArr = array_filter(explode(',',$id));
        $idArr =  array_map('intval', $idArr);
        $data['type'] = 1;
        $map['id'] = array('in',$idArr);
        $res = $this->where($map)->save($data);
        return $res;
    }

    public function getInfoByType($type){
        $map['type'] = $type;
        return $this->where($map)->limit(18)->select();
    }

    public function getHotInfo(){
        $map['valid_date'] = array('gt',date("Y-m-d H:i:s",time()));
        $field = array('id,title,area,linkman,money,content,cid,phone,tel,valid_date,thum,type');
        return $this->where($map)->field($field)->order('type desc')->limit(5)->select();
    }

}
